package edu.tongji.libraryapp;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.ListView;
import edu.tongji.libraryapp.adapter.SearchListAdapter;
import edu.tongji.libraryapp.model.Book;

public class SearchActivity extends Activity {

	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			Log.d("TAG", "[TextWatcher][onTextChanged]" + s);

		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);

		bindSearchFunction();
		initSearchListView();
	}

	private void initSearchListView() {
		ListView searchListView = (ListView) findViewById(R.id.searchListView);
		ArrayList<Book> searchList = new ArrayList<Book>();
		for (int i = 0; i < 5; i++) {
			searchList.add(new Book());
		}
		SearchListAdapter searchListAdapter = new SearchListAdapter(
				getApplicationContext(), this, searchList);
		searchListView.setAdapter(searchListAdapter);
	}

	private void bindSearchFunction() {
		EditText searchText = (EditText) findViewById(R.id.searchText);
		searchText.addTextChangedListener(watcher);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent searchIntent = new Intent();
			searchIntent.setClass(SearchActivity.this, MainActivity.class);
			startActivity(searchIntent);
			overridePendingTransition(R.anim.slide_in_right,
					R.anim.slide_out_right);
		}
		return true;
	}

}
